﻿Public Class FrmNhapHoSoDichVu
    Private Sub BatChucNang()
        txtSoHoSo.Enabled = True
        txtTenCongTrinh.Enabled = True
        dtNgayNhan.Enabled = True
        txtHoSoNam.Enabled = True
        txtThanhPhanHoSo.Enabled = True
        cmbHuyen.Enabled = True
        cmbXa.Enabled = True
        cmbPhong.Enabled = True
        cmbKe.Enabled = True
        cmbKho.Enabled = True
        '   cmbTinhTrang.Enabled = True
        btnLuu.Enabled = True
        ' LamTuoi()
        txtTenCongTrinh.Focus()
    End Sub
    Private Sub TatChucNang()
        txtSoHoSo.Enabled = False
        txtTenCongTrinh.Enabled = False
        dtNgayNhan.Enabled = False
        txtHoSoNam.Enabled = False
        txtThanhPhanHoSo.Enabled = False
        cmbHuyen.Enabled = False
        cmbXa.Enabled = False
        cmbPhong.Enabled = False
        cmbKe.Enabled = False
        cmbKho.Enabled = False
        '  cmbTinhTrang.Enabled = False
        btnLuu.Enabled = False
        ' LamTuoi()
    End Sub
    Private Sub LamTuoi()
        txtSoHoSo.Text = ""
        txtTenCongTrinh.Text = ""
        dtNgayNhan.Text = ""
        txtHoSoNam.Text = ""
        txtThanhPhanHoSo.Text = ""
    End Sub
    Private Function TimTenCongTrinh() As DataTable
        Dim sql As String
        sql = "Select * From CONGTRINH Where TenCongTrinh like N'" & txtTenCongTrinh.Text & "'"
        Return LoadThongTin(sql)
    End Function
    Private Function TimSoHoSo() As DataTable
        Dim sql As String
        sql = "Select * From CONGTRINH Where SoHoSo like N'" & txtSoHoSo.Text & "'"
        Return LoadThongTin(sql)
    End Function
    Private Sub NapDSCongTrinh()
        Dim sql As String
        sql = "SELECT CONGTRINH.SoHoSo, CONGTRINH.TenCongTrinh,  CONVERT(CHAR(10), CONGTRINH.NgayNhan, 103) AS NgayNhan, CONGTRINH.HoSoNam, CONGTRINH.ThanhPhanHoSo, " &
              "TENHUYEN.T_Huyen, TENXA.TenXa, DMPhong.TenPhong, DMKe.TenKe, DMKho.TenKho, DMTinhTrang.TenTinhTrang " &
              "FROM CONGTRINH LEFT OUTER JOIN " &
              "DMPhong ON congtrinh.MaPhongID = DMPhong.MaPhongID LEFT OUTER JOIN " &
              "DMKe ON CONGTRINH.MaKeID = DMKe.MaKeID LEFT OUTER JOIN " &
              "DMKho ON CONGTRINH.MaKhoID = DMKho.maKhoID LEFT OUTER JOIN " &
              "DMTinhTrang ON CONGTRINH.MaTinhTrangID = DMTinhTrang.MaTinhTrangID LEFT OUTER JOIN " &
              "TENXA ON CONGTRINH.MaXa = TENXA.MaXa LEFT OUTER JOIN " &
              "TENHUYEN ON TENHUYEN.maH = TENXA.MaHuyen"
        dgvDSCongTrinh.DataSource = LoadThongTin(sql)
        dgvDSCongTrinh.Refresh()
    End Sub
  
    Private Sub FrmNhapHoSoDichVu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        LoadHuyen(cmbHuyen)
        LoadXa(Convert.ToInt32(cmbHuyen.SelectedValue.ToString), cmbXa)
        LoadPhong(cmbPhong)
        LoadKe(cmbKe)
        LoadKho(cmbKho)
        LoadTinhTrang(cmbTinhTrang)

        dtNgayNhan.Text = Date.Today()
        NapDSCongTrinh()
        TatChucNang()
        btnNhap.Focus()
        dgvDSCongTrinh.RowsDefaultCellStyle.BackColor = Color.Beige
        dgvDSCongTrinh.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure

        ' KiemTraDock(Me)
    End Sub

    Private Sub cmbHuyen_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbHuyen.SelectedIndexChanged
        LoadXa(Convert.ToInt32(cmbHuyen.SelectedValue.ToString), cmbXa)
    End Sub

    Private Sub btnNhap_Click(sender As Object, e As EventArgs) Handles btnNhap.Click
        BatChucNang()
        'txtSoHoSo.Text = AutoID("CONGTRINH") + 1
        cmbTinhTrang.SelectedValue = 1 'Trang thai luu kho
        dtNgayNhan.Text = Date.Today()
    End Sub

    Private Sub btnLuu_Click(sender As Object, e As EventArgs) Handles btnLuu.Click
        Dim sql As String
        Dim tb As New DataTable

        'Kiem tra So ho so khong duoc rong
        If (txtSoHoSo.Text.Trim = "") Then
            MessageBox.Show("Số hồ sơ không được rỗng ")
            txtTenCongTrinh.Focus()
            Return
        End If

        'Kiem tra Ten cong trinh khong duoc rong
        If (txtTenCongTrinh.Text.Trim = "") Then
            MessageBox.Show("Tên Công trình không được rỗng ")
            txtTenCongTrinh.Focus()
            Return
        End If

        'Kiem tra Ho so nam khong duoc rong
        If (txtHoSoNam.Text.Trim = "") Then
            MessageBox.Show("Hồ sơ năm không được rỗng ")
            txtHoSoNam.Focus()
            Return
        End If
        'Kiem tra Thanh phan ho so khong duoc rong
        If (txtThanhPhanHoSo.Text.Trim = "") Then
            MessageBox.Show("Thành phần hồ sơ không được rỗng ")
            txtThanhPhanHoSo.Focus()
            Return
        End If
        'Kiem tra nam phai nho hon nam hien tai
        If (Integer.Parse(txtHoSoNam.Text) > Date.Today.Year) Then
            MessageBox.Show("Năm phải nhỏ hơn năm " & Date.Today.Year.ToString)
            txtHoSoNam.Focus()
            Return
        End If

        'Kiem tra khoa chinh
        tb = TimSoHoSo()
        If (tb.Rows.Count > 0) Then
            MessageBox.Show("Số hồ sơ này đã tồn tại")
            txtSoHoSo.Focus()
            Return
        End If

        'Kiem tra ten cong trinh da ton tai
        tb = TimTenCongTrinh()
        If (tb.Rows.Count > 0) Then
            If MessageBox.Show("Tên công trình đã có bạn có muốn tiếp tục lưu không", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.No Then
                Return
            End If
        End If
        Dim NgayNhan As String
        NgayNhan = String.Format("{0:MM/dd/yyyy}", dtNgayNhan.Value)
        sql = "INSERT INTO CONGTRINH " &
             "(SoHoSo,TenCongTrinh, NgayNhan, HoSoNam, ThanhPhanHoSo, MaXa,MaPhongID, MaKeID ,MaKhoID,MaTinhTrangID)" &
             "VALUES('" & txtSoHoSo.Text.Trim() & "',N'" & txtTenCongTrinh.Text.Trim() & "', '" & NgayNhan & "', " & txtHoSoNam.Text & ", N'" & txtThanhPhanHoSo.Text.Trim & "', " &
             "'" & cmbXa.SelectedValue.ToString() & "', '" & cmbPhong.SelectedValue.ToString() & "', '" & cmbKe.SelectedValue.ToString & "', '" & cmbKho.SelectedValue.ToString & "', '" & cmbTinhTrang.SelectedValue.ToString & "' ) "

        Dim flag As Boolean
        flag = ThaoTacDuLieu(sql)

        If (flag = True) Then
            MessageBox.Show("Lưu thông tin mới thành công")
            NapDSCongTrinh()
            LuuThongTinQuanLy()
            TatChucNang()
        End If

    End Sub

    Private Sub LuuThongTinQuanLy()
        Dim sql As String
        sql = "INSERT INTO QL_NhapCongTrinh (SoHoSo,NguoiNhap) VALUES ('" & txtSoHoSo.Text.Trim() & "', '" & ModuleTenDangNhap.m_tennhap & "')"
        Dim t As Boolean
        t = ThaoTacDuLieu(sql)
    End Sub

    Private Sub btnThoat_Click(sender As Object, e As EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub txtHoSoNam_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtHoSoNam.KeyPress

        If (Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar)) Then
            MessageBox.Show("Kí tự không được chấp nhận. Chỉ chấp nhận số")
            e.Handled = True
        End If
    End Sub

    Private Sub dgvDSCongTrinh_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles dgvDSCongTrinh.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < dgvDSCongTrinh.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If dgvDSCongTrinh.RowHeadersWidth < Size.Width + 20 Then dgvDSCongTrinh.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub

    Private Sub grbThongTinCongTrinh_Enter(sender As Object, e As EventArgs) Handles grbThongTinCongTrinh.Enter

    End Sub

    Private Sub cmbXa_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbXa.SelectedIndexChanged

    End Sub
End Class